package de.grosjohann.android.feuerwehr.fms;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.client.ClientProtocolException;

import android.content.Context;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.util.PrefsUtil;

public class FMSWsFacade {

  public static String getFMSStatus(Context ctx) throws ClientProtocolException, IOException, HttpException {
    String baseUri = ctx.getString(R.string.homepage);
    final String wsUri = "/fms/status";

    baseUri = "http://IW01721100.system.local:8082/webservices";

    FMSWsClient client = createClient(ctx, baseUri + wsUri);
    return client.get();
  }

  public static String setFMSStatus(Context ctx, String authorization, String kennung, String status)
    throws ClientProtocolException, IOException, HttpException {
    String baseUri = ctx.getString(R.string.homepage);
    final String wsUri = "/fms/status";

    FMSWsClient client = createClient(ctx, baseUri + wsUri + "/" + kennung);
    return client.post(authorization, status);
  }

  public static String getFMSAuthorization(Context ctx, String authorization) throws ClientProtocolException,
    IOException, HttpException {
    String baseUri = ctx.getString(R.string.homepage);
    final String authUri = "/fms/authorization";

    baseUri = "http://IW01721100.system.local:8082/webservices";

    FMSWsClient client = createClient(ctx, baseUri + authUri + "/" + authorization);
    return client.get();
  }

  private static FMSWsClient createClient(Context ctx, String uri) throws ClientProtocolException, IOException {
    final int connectionTimeout =
        PrefsUtil.getInt(ctx, R.string.prefKeyConnectionTimeout, R.string.prefDefaultConnectionTimeout);
    final int socketTimeout = PrefsUtil.getInt(ctx, R.string.prefKeySocketTimeout, R.string.prefDefaultSocketTimeout);

    return new FMSWsClient(uri, connectionTimeout, socketTimeout);
  }
}
